<template>
  <div style="height:100%;width:100%;position:relative;">
      <div class="pagetop" ref="buttondiv">
          <template>
              <table width="100%">
                  <tr>
                      <td align="right" width="60px">修改日期</td>
                      <td width="280px">
                          <el-date-picker v-model="modifydate1" type="date" placeholder="开始日期" value-format="yyyy-MM-dd" style="width:130px"></el-date-picker> 至
                          <el-date-picker v-model="modifydate2" type="date" placeholder="结束日期" value-format="yyyy-MM-dd" style="width:130px"></el-date-picker>
                      </td>
                      <td align="right" width="60px">仪器</td>
                      <td width="220px">
                          <rm-ddl-instrument @change="labinstrumentchange" width="220px" v-model="instrId" defaultflag="1" prgno="LIS529" :clearable="false"></rm-ddl-instrument>
                      </td>
                      <td align="right" width="40px">批号</td>
                      <td width="120px">
                          <el-input v-model="batchno" id="batchno" style="width:120px"></el-input>
                      </td>
                      <td align="right" width="50px">修改人</td>
                      <td width="200px">
                          <rm-ddl-common datatype="userinfo" dataname="用户" v-model="modifyuser" width="200px"></rm-ddl-common>
                      </td>
                            <td align="right" width="50px">修改类型</td>
                      <td width="200px">
                          <el-select v-model="operatetype" @change="saveselect">
                                      <el-option v-for="item in $opt.operatetypeOption" :label="item.label" :key="item.value" :value="item.value"></el-option>
                                  </el-select>
                      </td>
                  </tr>
                  <tr>
                      <td align="right" width="60px">质控日期</td>
                      <td width="280px">
                          <el-date-picker v-model="qcdate1" type="date" placeholder="开始日期" value-format="yyyy-MM-dd" style="width:130px"></el-date-picker> 至
                          <el-date-picker v-model="qcdate2" type="date" placeholder="结束日期" value-format="yyyy-MM-dd" style="width:130px"></el-date-picker>
                      </td>

                      <td align="right">质控项目</td>
                      <td>
                          <rm-ddl-common datatype="qcinstritem" dataname="仪器对应质控项目" v-model="qcitem" :instrid="parseInt(instrId)" width="220px"> </rm-ddl-common>
                      </td>
                      <td align="right">水平</td>
                      <td width="120px">
                          <rm-ddl-common datatype="seccode" kindno="QCLEVEL" dataname="水平" v-model="qclevel" width="120px"></rm-ddl-common>
                      </td>
                      <td align="right">修改原因</td>
                      <td width="150px">
                          <el-input v-model="modifyreason" id="modifyreason" style="width:200px"></el-input>
                      </td>
                      <td></td>         <td></td>
                  </tr>
                  <tr>
                      <td colspan="11" style="text-align:right">
                          <el-button type="primary" icon="el-icon-search" @click="initialList" v-has="'PINQUERY'">查询(F8)</el-button>
                          <el-button icon="el-icon-download" @click.native="BtnExport">导出(E)</el-button>
                          <el-button icon="el-icon-close" @click="BtnExc">退出(X)</el-button>
                      </td>
                  </tr>
              </table>
          </template>
      </div>
      <div class="pagecenter">
          <div ref="xTableDiv" style="height:99%;width:100%">
              <vxe-table show-overflow ref="xTable1" height="auto" width="100%" :loading="loading" :select-config="{ reserve: true }" :data.sync="tableData1" border size="mini" :keyboard-config="keyConfig" :mouse-config="{ selected: true }" show-header-overflow highlight-current-row highlight-hover-row resizable stripe :edit-config="{ trigger: 'click', mode: 'cell', showStatus: true }">
                  <vxe-table-column field="instrname" title="仪器" width="120px" header-align="center">
                  </vxe-table-column>
                  <vxe-table-column field="qcdate" title="质控日期" width="100px" align="center" :formatter="['toDateString', 'yyyy-MM-dd']">
                  </vxe-table-column>
                  <vxe-table-column field="itemcode_en" title="英文缩写" width="100" header-align="center"></vxe-table-column>
                  <vxe-table-column field="rpt_itemname" title="项目名称" width="150" header-align="center"></vxe-table-column>
                  <vxe-table-column field="batchno" title="批号" width="100" header-align="center"></vxe-table-column>
                  <vxe-table-column field="qclevel" title="水平" width="50" align="center"></vxe-table-column>
                  <vxe-table-column field="qcvaluesign" title="结果标识" width="150" header-align="center"></vxe-table-column>
                  <vxe-table-column field="operatetype" title="结果类型" width="100" align="center" :formatter="['formatSelect', operatetypeList]"></vxe-table-column>
                  <vxe-table-column field="modifyreason" title="修改原因" width="150" header-align="center"></vxe-table-column>
                  <vxe-table-column field="modifydt" title="修改时间" width="140" align="center" :formatter="['toDateString', 'yyyy-MM-dd HH:mm:ss']"></vxe-table-column>
                  <vxe-table-column field="cp_modifyuser" title="修改人" width="60" align="center"></vxe-table-column>
                  <vxe-table-column field="olddisplayvalue" title="原显示结果" width="100" header-align="center" align="right"></vxe-table-column>
                  <vxe-table-column field="oldcalculatevalue" title="原计算结果" width="100" header-align="center" align="right"></vxe-table-column>
                  <vxe-table-column field="newdisplayvalue" title="新显示结果" width="100" header-align="center" align="right"></vxe-table-column>
                  <vxe-table-column field="newcalculatevalue" title="新计算结果" width="100" header-align="center" align="right"></vxe-table-column>
                  <vxe-table-column field="qcname" title="质控品名称" width="150" header-align="center"></vxe-table-column>
                  <vxe-table-column field="qcid" title="质控品ID" width="100" header-align="center"></vxe-table-column>
              </vxe-table>
          </div>
      </div>
  </div>
</template>

<script type="text/javascript">
import XEUtils from "xe-utils"; //引入工具类
XEUtils.mixin({
  // 自定义全局的格式化处理函数
  formatSelect(cellValue, list) {
      let item = list.find((item) => item.value == cellValue);
      return item ? item.label : "";
  },
});
export default {
  name: "LIS529",
  data() {
      return {
          loading: false,
          instrId: 0,
          qcdate1: "",
          qcdate2: "",
          modifydate1: "",
          modifydate2: "",
          tableData1: [],
          qcitem: "",
          batchno: "",
          qclevel: "",
          modifyreason: "",
          modifyuser: "",
          operatetype:"",
          qclevelOption: [
              { label: "全部", value: 0 },
              { label: "1", value: 1 },
              { label: "2", value: 2 },
              { label: "3", value: 3 },
              { label: "4", value: 4 },
              { label: "5", value: 5 },
              { label: "6", value: 6 },
          ],
          controlstatusList: [
              { label: "失控", value: "0" },
              { label: "在控", value: "1" },
              { label: "警告", value: "3" },
              { label: "未判定", value: null },
          ],
          operatetypeList: [
              {label:"手工新增",value:"A"},
              { label: "修改结果", value: "M" },
              { label: "删除数据", value: "D" },
              { label: "审核", value: "C" },
              { label: "解除审核", value: "U" },
          ],
          dgList: [
              { label: "", value: "0" },
              { label: "√", value: "1" },
          ],

          keyConfig: {
              isArrow: true,
              isDel: true,
              isTab: true,
              isEdit: true,
              editMethod: ({ seq, row, rowIndex, column, columnIndex, cell }) => {},
          },
      };
  },

  //html加载完成之前，执行。执行顺序：父组件-子组件
  created() {
      this.modifydate1 = XEUtils.toDateString(new Date(), "yyyy-MM-dd"); //起 始结束日期
      this.modifydate2 = XEUtils.toDateString(new Date(), "yyyy-MM-dd");
  },
  //.html加载完成后执行。执行顺序：子组件-父组件
  mounted() {
      let modOption1 = localStorage.getItem("rm_Qmod_LIS529");
      let modjson = JSON.parse(modOption1);
      if (modjson != null) {
          this.operatetype = modjson.operatetype;
      }
  },
  updated() {},
  methods: {
         BtnExport() {
          if (this.$refs.xTable1.afterFullData.length > 0) {
              let tableColumn = [];
              let extableColumn = [];
              //导出结果
              tableColumn = this.$refs.xTable1.getColumns();
              tableColumn.forEach((item) => {
                  extableColumn.push({ title: item.title, field: item.property });
              });
              let exportData = XEUtils.clone(this.tableData1, true);
              this.$pub.exportTableExcel(exportData, extableColumn, "LIS529 质控数据修改记录查询.xlsx");
          } else {
              this.$alert("请先查询出有效数据。", "提示", {
                  confirmButtonText: "确定",
                  type: "warning",
              });
              return false;
          }
      },
      //初始化数据
      resetData() {
          this.tableData1 = [];
      },
      //记忆类型
       saveselect() {
          let jsondate = {
              operatetype: this.operatetype,
          };
               let modListJson = JSON.stringify(jsondate);
          localStorage.setItem("rm_Qmod_LIS529", modListJson);
      },
      //查询条件校验
      queryCheck() {
          let msg;
          this.instrId = this.instrId > 0 ? this.instrId : undefined;
          msg = this.valid(this.instrId, "请选择仪器！"); //仪器校验
          if (msg) return false;
          msg = this.valid(this.qcdate1, "开始日期 必须填写！"); //时间校验
          if (msg) return false;
          msg = this.valid(this.qcdate2, "结束日期 必须填写！");
          if (msg) return false;
          return true;
      },

      initialList() {
          //主表查询
          //if (this.queryCheck() === false) return;
          this.resetData();
          this.loading = true;
          this.$tool
              .request({
                  url: "LIS/LIS5/LIS529",
                  method: "post",
                  data: {
                      action: "query",
                      instrid: this.instrId,
                      modifydate1: this.modifydate1,
                      modifydate2: this.modifydate2,
                      qcdate1: this.qcdate1,
                      qcdate2: this.qcdate2,
                      qcitem: this.qcitem,
                      batchno: this.batchno,
                      qclevel: this.qclevel,
                      modifyuser: this.modifyuser,
                      modifyreason: this.modifyreason,
                      operatetype:this.operatetype,
                  },
              })
              .then((res) => {
                  if(res.errcode =="1")
                  {
                          this.tableData1 = res.Data;
                  }else if(res.errcode =="0")
                  {
                         this.$alert(res.errmsg, "错误");
                  }
                  this.loading = false;
              })
              .catch((e) => {
                  this.loading = false;
              });
      },

      //切换仪器时刷新界面
      labinstrumentchange(id, name) {
          this.instrId = id;
          this.$nextTick(() => {
              //this.initialList();
          });
      },

      ///////////////////////////////////工具函数 /////////////////////////////////////////
      getDataByAjax(
          request,
          okMsg,
          errMsg,
          okFunction, // 取得数据后的回调函数
          errFunction // 获取数据失败后回调函数
      ) {
          this.$tool.request(request).then((response) => {
              if (response.errcode == "1") {
                  if (XEUtils.isString(okMsg)) {
                      if (XEUtils.getSize(XEUtils.trim(okMsg)) > 0) {
                          this.$message({
                              message: okMsg,
                              type: "success",
                          });
                      }
                  }
                  if (typeof okFunction == "function") okFunction(response.Data);
              } else {
                  this.$message({
                      dangerouslyUseHTMLString: true,
                      message: errMsg + "<br><br>详细信息：" + response.errmsg,
                      type: "warning",
                  });
                  if (typeof errFunction == "function") errFunction(response.Data);
              }
          });
      },

      insertRowAtLast(xtable, row) {
          xtable.insertAt(row, -1); //在最后添加一行
          var curow = xtable.getTableData().fullData[xtable.getTableData().fullData.length - 1]; //获取 新加的行对象 row 数据集合
          xtable.setCurrentRow(curow); //高亮显示行
          this.$nextTick(() => {
              xtable.scrollToRow(curow); //待数据加载完成后，延迟 滚动 到行
          });
      },

      //自动加载表格的第一行，并执行回调函数
      loadFirstRow(xTable, func) {
          this.$nextTick(() => {
              // 加载第一行
              var selectrow = xTable.getData(0);
              if (selectrow) {
                  xTable.setCurrentRow(selectrow); //高亮行
                  xTable.scrollToRow(selectrow); //滚动到行
                  if (typeof func == "function") func(selectrow);
              } else {
                  xTable.setCurrentRow(null); //这个不会自动清空
              }
          });
      },

      //自制必填校验函数，以后待扩展
      valid(obj, message) {
          if (XEUtils.isUndefined(obj)) {
              return this.$message({
                  message: message,
                  type: "warning",
              });
          }
          if (XEUtils.isNull(obj)) {
              return this.$message({
                  message: message,
                  type: "warning",
              });
          }
          if (XEUtils.isString(obj)) {
              if (!XEUtils.getSize(obj) > 0) {
                  return this.$message({
                      message: message,
                      type: "warning",
                  });
              }
          }
          if (XEUtils.isDate(obj)) {
              if (obj < XEUtils.toStringDate("2000-01-01")) {
                  return this.$message({
                      message: message,
                      type: "warning",
                  });
              }
          }
      },

      //删除表格行工具//TODO 连续 点删除按钮会有失去焦点的问题待解决
      deleteRow(xTable) {
          var delrow = xTable.getCurrentRow();
          this.DelIndex = xTable.getRowIndex(delrow);
          xTable.remove(delrow);
          if (this._this.DelIndex > -1 && xTable.tableData.length > 0) {
              if (xTable.getTableData().fullData.length > this.DelIndex) this.DelIndex = this.DelIndex;
              elsethis.DelIndex = this.DelIndex - 1;
              var selectrow = xTable.getTableData().fullData[this._this.DelIndex];
              this.$nextTick(() => {
                  xTable.setCurrentRow(selectrow);
              });
          } else {
              var nowdata = xTable.getTableData().fullData.filter((item) => {
                  return item._XID < delrow._XID;
              });
              if (nowdata.length > 0) {
                  var selectrow = nowdata[nowdata.length - 1];
                  this.$nextTick(() => {
                      xTable.setCurrentRow(selectrow);
                  });
              }
          }
      },

      stddevp(numbers) {
          var mean = 0;
          var sum = 0;
          var avgValue, sdValue, cvValue;
          for (var i = 0; i < numbers.length; i++) {
              sum += Number(numbers[i]);
          }
          mean = sum / numbers.length;
          avgValue = mean;

          sum = 0;
          for (var i = 0; i < numbers.length; i++) {
              sum += Math.pow(numbers[i] - mean, 2);
          }
          sdValue = Math.sqrt(sum / numbers.length);

          if (avgValue != 0) {
              cvValue = (sdValue * 100) / avgValue;
          } else {
              cvValue = null;
          }
          return {
              avgValue: XEUtils.toFixedString(mean, 2),
              sdValue: XEUtils.toFixedString(sdValue, 2),
              cvValue: XEUtils.toFixedString(cvValue, 2),
          };
      },
      //关闭按钮
      BtnExc() {
          let index = this.$parent.$refs.itemtags.tagsList.findIndex((m) => m.name == "LIS529");
          this.$parent.$refs.itemtags.tagsList.splice(index, 1);
          this.$router.go(-1);
      },
  },
  beforeRouteEnter(to, from, next) {
      next((vm) => {
          let that = vm;
          let altArr = [];
          document.onkeydown = function (e) {
              let key = window.event.keyCode;
              console.log("key=", key);
              //*组合按键
              if (altArr.length > 0) {
                  //65到90 a到z（A到Z）
                  // //*Alt + C 清除
                  // if (key == 67) {
                  //     this.BtnExc();
                  // }
                  if (key == 88) {
                      console.log("🕌🛕🕍11");
                      //*Alt + X 退出
                      that.BtnExc();
                  }
              } else {
                  if (key == 119) {
                      //F8
                      that.initialList();
                   } else if (key == 18) {
                      console.log("按下'ALT'");
                      //按下了alt
                      altArr.push(key);
                  }else  if (key == 69) {
                      that.BtnExport();
                  }
              }
          };
          document.onkeyup = function (e) {
              let key = window.event.keyCode;
              if (key == 18) {
                  altArr = [];
              }
          };
      });
  },
};
</script>

<style scoped>
.pagetop {
  left: 0;
  right: 0;
  height: 99px;
  display: -webkit-box;
  -webkit-box-pack: left;
  -webkit-box-align: center;
  border: solid 1px #ccc;
  position: absolute;
  z-index: 99;
  text-align: left;
}
.pagebottom {
  width: 100%;
  height: 40px;
  display: -webkit-box;
  -webkit-box-pack: left;
  -webkit-box-align: left;
  position: absolute;
  bottom: 0px;
  font-size: 12px;
  border: solid 1px #ccc;
}
.pagecenter {
  position: absolute;
  top: 104px;
  width: 100%;
  left: 0;
  bottom: 0px;
  overflow: auto;
}
.middle-center {
  position: absolute;
  height: 100%;
  right: 485px;
  left: 0;
}

.middle-right {
  position: absolute;
  top: 0;
  bottom: 2px;
  overflow: visible;
  width: 480px;
  right: 0px;
}

table tr td:nth-child(1) {
  width: 80px;
}
table {
  font-size: 12px;
}
::v-deep .setoutcontrolcolor {
  background-color: #ff6666 !important;
}
::v-deep .setoutcontrolcolorNull {
  background-color: #cccccc !important;
}
</style>
